import { fetch, grabPage, finish } from "./utils/gotFetch.mjs";
import { clean, getOrigin } from "./utils/index.mjs";
import { netSite } from "./config.mjs";
const {
  name,
  homePath,
  links,
  selector,
  next,
  pageName,
  getTitle,
  keyword,
  maxPage,
  isOrigin = false,
} = netSite;

// 处理页面获取图片
async function handlePage(url, title, page = 1) {
  title = clean(title);
  page == 1 && console.log(`进入图集:${title}`, url);
  console.log(`处理页面:page${page}`);
  // 解析页面
  const target = url.includes("http")
    ? url
    : isOrigin
    ? getOrigin(homePath) + url
    : homePath.slice(0, homePath.lastIndexOf("/")) + url;
  const $ = await grabPage(target);
  // 下载页面所有图片
  await fetch($, {
    selector,
    pageName: `page${page}-`,
    directory: `./assets/${name}/${title}/`,
  });
  // 获取下一页链接
  let nextPage = typeof next === "function" ? next($) : $(next).attr("href");
  if (!nextPage) return;
  if (!nextPage.includes("http")) {
    if (isOrigin) {
      nextPage = getOrigin(homePath) + nextPage;
    } else {
      nextPage = nextPage.startsWith("/") ? nextPage.slice(1) : nextPage;
      nextPage = url.substring(0, url.lastIndexOf("/") + 1) + nextPage;
    }
    console.log("nextPage", nextPage);
  }
  return handlePage(nextPage, title, page + 1);
}

// 抓取网站的图集页面
async function fetchPages(maxPage = 1, keyword = "") {
  const rule = new RegExp(keyword, "i");
  for (let i = 1; i <= maxPage; i++) {
    const $ = await grabPage(homePath + pageName(i));
    // 获取图集的链接数组
    const imgElements = $(links).toArray();
    for (let index = 0; index < imgElements.length; index++) {
      const element = imgElements[index];
      const title = getTitle($(element)).trim().substring(0, 25);
      const href = $(element).attr("href");
      if (title.match(rule)) await handlePage(href, title);
    }
  }
  finish();
}

// 获取首页所有图集链接
fetchPages(maxPage, keyword);
